package com.vxb.testmvp.test_fragment.fm_a;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.a26c.android.frame.widget.BaseRecyclerView;
import com.a26c.android.frame.widget.MutiItemDecoration;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.holenzhou.pullrecyclerview.BaseRecyclerAdapter;
import com.holenzhou.pullrecyclerview.PullRecyclerView;
import com.holenzhou.pullrecyclerview.layoutmanager.XLinearLayoutManager;
import com.vxb.testmvp.R;
import com.vxb.testmvp.test_fragment.BaseFragment;
import com.vxb.testmvp.test_fragment.LoadingPager;
import com.vxb.testmvp.test_fragment.activity.TestModel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by User on 2017/3/1.
 */

public class FragmentA extends BaseFragment {
    private View view;
    private BaseRecyclerView baseRecyclerView;
    private PullRecyclerView mPullRecyclerView;

    private List<CheesesItem> mDataList = new ArrayList<>();
    private BaseRecyclerAdapter<CheesesItem> mAdapter;
    private int pageSize = 2;

    public RequestQueue mQueue;


    private void testFram() {
        List<TestBaseAdapterData> list = new ArrayList<>();
        TestBaseAdapter adapter = new TestBaseAdapter(list);
        baseRecyclerView.init(adapter, new BaseRecyclerView.NetworkHandle() {
            @Override
            public void init(BaseRecyclerView baseRecyclerView) {
                baseRecyclerView.openLoadMore(20);
                baseRecyclerView.setNodataLayoutId(R.layout.layout_empty);
                baseRecyclerView.setErrLayoutId(R.layout.layout_err);
            }

            @Override
            public void loadData(boolean isRefresh, final String pageIndex) {
                baseRecyclerView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        baseRecyclerView.onLoadDataComplete(TestModel.getTestList(pageIndex));
                    }
                }, 1000);
            }
        });
        baseRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 5));
        baseRecyclerView.removeDivider();
        baseRecyclerView.addDivider(new MutiItemDecoration(MutiItemDecoration.Type.ALL, 5, 0xff00ff00));
    }

    private void testPullRefresh() {
        // 初始化PullRecyclerView
        mPullRecyclerView.setLayoutManager(new XLinearLayoutManager(getActivity()));

        mAdapter = new CommonListAdapter(getActivity(), R.layout.list_item, mDataList);
        mPullRecyclerView.setAdapter(mAdapter);


        mPullRecyclerView.setColorSchemeResources(R.color.colorAccent); // 设置下拉刷新的旋转圆圈的颜色
        DividerItemDecoration itemDecoration = new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL);
        itemDecoration.setDrawable(getResources().getDrawable(R.drawable.shape_simple_item_decoration));
        mPullRecyclerView.addItemDecoration(itemDecoration);

        mPullRecyclerView.enablePullRefresh(true); // 开启下拉刷新，默认即为true，可不用设置
        mPullRecyclerView.enableLoadDoneTip(true, R.string.load_done_tip); // 开启数据全部加载完成时的底部提示，默认为false
        mPullRecyclerView.setOnRecyclerRefreshListener(new PullRecyclerView.OnRecyclerRefreshListener() {
            @Override
            public void onPullRefresh() {
                // 模拟下拉刷新网络请求
                mPullRecyclerView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mDataList.clear();
                        mDataList.addAll(Cheeses.getRandomSubList(30));
                        mAdapter.notifyDataSetChanged();
                        // 或者直接使用BaseRecyclerAdapter中封装的方法
                        //mAdapter.replaceAll(mDataList);
                        mPullRecyclerView.stopRefresh();
                        mPullRecyclerView.enableLoadMore(true); // 当剩余还有大于0页的数据时，开启上拉加载更多
                        pageSize = 2;
                    }
                }, 1500);
            }

            @Override
            public void onLoadMore() {
                pageSize--;
                // 模拟上拉加载更多网络请求
                mPullRecyclerView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mDataList.addAll(Cheeses.getRandomSubList(30));
                        mAdapter.notifyDataSetChanged();
                        // 或者直接使用BaseRecyclerAdapter中封装的方法
                        //mAdapter.addAll(mDataList);

                        mPullRecyclerView.stopLoadMore();
                        mPullRecyclerView.enableLoadMore(pageSize > 0);
                    }
                }, 1500);
            }
        });
        mPullRecyclerView.postRefreshing();

        initData();
    }

    private void initData() {
        mQueue = Volley.newRequestQueue(getActivity());
        //在网络请求回调里面 返回 .
        StringRequest stringRequest = new StringRequest("http://www.baidu.com",
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.d("TAG", response);
                        mAdapter = new CommonListAdapter(getActivity(), R.layout.list_item, mDataList);
                        mPullRecyclerView.setAdapter(mAdapter);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e("TAG", error.getMessage(), error);
                        Snackbar.make(view, "Hello SnackBar!", Snackbar.LENGTH_SHORT)
                                .setAction("Undo", new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        // Perform anything for the action selected
                                    }
                                })
                                .setActionTextColor(getResources().getColor(R.color.colorPrimary))
                                .setDuration(4000).show();
                    }
                });
        mQueue.add(stringRequest);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_a, null);
        baseRecyclerView = (BaseRecyclerView) view.findViewById(R.id.base_recycler);
        mPullRecyclerView = (PullRecyclerView) view.findViewById(R.id.pull_recycler_view);


        // testFram();
        testPullRefresh();

        return view;
    }

    @Override
    protected View createSuccessView() {
        return null;
    }

    @Override
    protected LoadingPager.LoadResult load() {
        return null;
    }


}
